#include "processbar.h"
#include <unistd.h>
const char* lable = "|\\-/";

// version 1
void process()
{
    char buffer[NUM] = {'\0'};
    
    int count = 0;
    buffer[0] = Head;
    while(count<=100)
    {
        //打印字符数组
        printf("[%-100s][%3d%%] %c\r",buffer,count,lable[count%4]);
        //打印执行后要立即刷新屏幕，让打印内容出现在屏幕上
        fflush(stdout);
        buffer[count++] = Body;
        if(count < 100)
        {
            buffer[count] = Head;
        }
        usleep(50000);
    }
    printf("\n");
}

// version 2

static char buffer[NUM] = {'\0'};
static int  count = 0;
void process_flush_a(double rate)
{
    int temp = (int)rate;
    if(temp < 1) buffer[0] = Head;
    printf("[%-100s][%.1f%%] %c\r",buffer,rate,lable[count%4]);
    fflush(stdout);

    buffer[temp++] = Body;
    if(temp < 100)
    {
        buffer[temp] = Head;
    }

    count++;
    count %= 4;

}



// version 3

void process_flush_b(double rate)
{
    int temp = (int)rate;
    
    // 先打印白色背景的进度条底色
    printf("\033[48;5;7m");  // 设置背景为白色
    for (int i = 0; i < 100; i++) {
        printf(" ");
    }
    printf("\033[0m\r");  // 重置颜色并回到行首
    
    // 打印紫色进度部分
    printf("\033[48;5;135m");  // 设置背景为紫色
    for (int i = 0; i < temp; i++) {
        printf(" ");
    }
    printf("\033[0m");  // 重置颜色
    
    // 打印百分比和旋转符号
    printf("[%.1f%%] %c\r", rate, lable[count % 4]);
    
    fflush(stdout);

    count++;
    count %= 4;
}
